package com.zpark.it.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zpark.it.pojo.Grade;
import com.zpark.it.service.GradeService;
import com.zpark.it.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Title: GradeController
 * @Author Charles
 * @Package com.zpark.it.controller
 * @Date 2023/7/30 21:33
 */
@Api(tags = "年级控制器")
@RestController
@RequestMapping("/sms/gradeController")
public class GradeController {

    @Autowired
    private GradeService gradeService;

    /**
     * 分页查询
     * @param pageNo
     * @param pageSize
     * @param gradeName
     * @return
     */
    @ApiOperation("根据年级名称模糊查询，分页查询")
    @GetMapping("/getGrades/{pageNo}/{pageSize}")
    public Result getGrades(@ApiParam("分页查询的页码")@PathVariable("pageNo") Integer pageNo,
                            @ApiParam("分页查询的页大小")@PathVariable("pageSize") Integer pageSize,
                            @ApiParam("分页查询模糊匹配的名称") String gradeName){
        //分页带条件查询
        Page<Grade> page=new Page<>(pageNo,pageSize);
        //通过服务层完成
        IPage<Grade> pageRs = gradeService.getGradeByOpr(page,gradeName);

        //封装result对象返回
        return Result.ok(pageRs);

    }

    /**
     * 添加或修改年级
     * @param grade
     * @return
     */
    @ApiOperation("新增或者修改年级，有id属性则修改，没有则是增加")
    @PostMapping("/saveOrUpdateGrade")
    public Result saveOrUpdateGrade(@ApiParam("json的年级对象")@RequestBody Grade grade){
          gradeService.saveOrUpdate(grade);
        return Result.ok();
    }

    /**
     * 删除年级
     * @param ids
     * @return
     */
    @ApiOperation("删除年级信息")
    @DeleteMapping("/deleteGrade")
    public Result deleteGrade(@ApiParam("需要删除的所有的年级的id的json的集合") @RequestBody List<Integer> ids){
        gradeService.removeByIds(ids);
        return Result.ok();

    }


    /**
     * 获取全部年级
     * @return
     */
    @ApiParam("获取全部年级")
    @GetMapping("/getGrades")
    public Result getGrades(){

        List<Grade> grades = gradeService.getGrades();
        return Result.ok(grades);
    }

}
